const api_url = "https://fanyi-api.baidu.com/api/trans/vip/translate";
const config = require('../data/config.js').trans;
let MD5 = getApp()._init({})._md5;

function to(words,to, call){
  let salt = (new Date).getTime();
  let sign = MD5(config.appid + words + salt + config.secret);
  let data = { appid: config.appid, q: words,from: 'auto',to:to,salt: salt,sign: sign}

  let transCallBack = function(res){
    try{
      let transResult = "";
      let transResultArray = res.data.trans_result.map(function(v,k){
        transResult += v.dst;
      });
      call(transResult);
    } catch(e){
      let transResult = words;
      call(transResult);      
    }
  }
  wx.request({ url: api_url, data: data, success: transCallBack})
}
module.exports.to = to;